import { ref } from 'vue'
import { defineStore } from 'pinia'
import { loginapi } from '@/apis/user.js'
import { ElMessage } from 'element-plus'
import 'element-plus/theme-chalk/el-message.css'
//导入路由包
import { useRouter } from 'vue-router';

import {usecatecart} from '@/stores/cate.js'
export const userstore =  defineStore('user',()=>{
  const useCatecart  = usecatecart()
    const router = useRouter()
    const userinfo = ref({})
    const getuserinfo = (async(formdata)=>{
    const res = await loginapi(formdata.value)
            //提示登录成功
            userinfo.value = res.result
            ElMessage({
              type: 'success',
              message:'登陆成功'
            })
            router.replace({path:'/'})
            useCatecart.getcartlist()
    })
    const clearuser = ()=>{
      userinfo.value ={}
      useCatecart.clearlist()
    }
    return {userinfo,getuserinfo,clearuser}
},{
  persist:true
})